/**
 * Copyright(c) 2025 hbswxx.com, All Rights Reserved. Author: kouqiang Create date: 2025/6/3
 */
package cn.sw.syncdata.job.heart;

import cn.sw.syncdata.entity.Port;
import cn.sw.syncdata.util.Util;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Random;
import java.util.UUID;

/**
 * description: AI人像数据创建镜像
 *
 * @author kouqiang
 * @date 2025/6/3 09:17
 */
@Slf4j
@RequiredArgsConstructor
public class ThreadCreatePort implements Runnable {

    private final int limit;
    private final String filename;
    private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    private final String sdate;
    private final String edate;

    @Override
    public void run() {
        Random rand = new Random();
        HashSet<Port> hashSet = new HashSet<>();
        LocalDateTime sDateTime = LocalDateTime.parse(sdate + "T00:00:00", formatter);
        LocalDateTime eDateTime = LocalDateTime.parse(edate + "T23:59:59", formatter);
        StringBuffer sb = new StringBuffer();
        sb.append("indexcode, collectionmodeindexcode, upperage, lowerage, genderindexcode, nationindexcode, controlpersonnel, followpersonnel, aismartmonitorindexcode, numberpeers, wearmask, leftmiddlehorizontal, leftmiddlelongitudinal, rightmiddletransverse, rightmiddlelongitudinal, hatindexcode, driver, glassesindexcode, carryingbagindexcode, topdressingindexcode, lowerdressingindexcode, shoesindexcode, coatcolor, trouserscolor, shoescolor, hatcolor, maskcolor, collectiontime, community_indexcode, emotionindexcode, beardindexcode, eyeindexcode, mouthindexcode, importtime, live_face, driver_id, GDC_TYPE ");
        sb.append("\n");
        for (int i = 0; i < limit; i++) {
            Port port = new Port();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            port.setIndexcode("swid" + uuid);
            LocalDateTime newDateTime = Util.randomBetween(sDateTime, eDateTime);
            int lowerage = rand.nextInt(30) + 15;
            port.setUpperage(String.valueOf(lowerage += rand.nextInt(20)));
            port.setLowerage(String.valueOf(lowerage));
            port.setGenderindexcode(String.valueOf(rand.nextInt(3)));
            port.setCommunity_indexcode("mlcommunityid_" + (rand.nextInt(99) + 1));
            String manufacturers = rand.nextInt(2) > 0 ? "HK" : "DH";
            port.setAismartmonitorindexcode(port.getCommunity_indexcode() + "#" + manufacturers + "#" + (rand.nextInt(5) + 1));
            port.setWearmask(String.valueOf(rand.nextInt(4)));
            port.setGlassesindexcode(String.valueOf(rand.nextInt(3)));
            port.setCollectiontime(newDateTime);
            port.setEmotionindexcode(String.valueOf(rand.nextInt(15)));
            if ("2".equals(port.getGenderindexcode())) {
                port.setBeardindexcode(String.valueOf(rand.nextInt(4)));
            } else
                port.setBeardindexcode(String.valueOf(rand.nextInt(3)));
            port.setEyeindexcode(String.valueOf(rand.nextInt(4)));
            port.setMouthindexcode(String.valueOf(rand.nextInt(4)));
            port.setImporttime(newDateTime.plusSeconds(rand.nextInt(600)));
            if (port.getImporttime().isAfter(eDateTime)) {
                port.setImporttime(eDateTime);
            }
            String liveFace = String.format("e:/live_face/AIImg/%1$tY/%1$tm/%1$td/%2$s.jpg",
                    port.getImporttime(),
                    port.getIndexcode());
            port.setLive_face(liveFace);
            sb.append(port);
            sb.append("\n");
            if (i % 2000 == 0) {
                wrteList(sb.toString());
            //    wrteList(sb.substring(0,sb.length()-1));
                sb.setLength(0);
            }
        }
        if(sb.length()>0)
        {
            wrteList(sb.substring(0,sb.length()-1));
            sb.setLength(0);
        }
    }

    private void wrteList(String lines) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {
            writer.write(lines);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
